package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.service.IRoleService;
import cn.wolfcode.util.Request;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IPermissionService permissionService;


    @Request(name = "角色列表",expression = "role.list")
    @RequestMapping("/list")
    public String list(Model model,QueryObject qo){
        PageInfo<Role> rolePageInfo = roleService.queryList(qo);

        model.addAttribute("pageResult",rolePageInfo);
        return "role/list";
    }


    @RequestMapping("/input")
    public String inpput(Model model, Long id){
        if (id != null){
            Role role1 = roleService.get(id);
            List listPerAndRoel = roleService.selectPerAndRoel(id);
            model.addAttribute("listPerAndRoel",listPerAndRoel);
            model.addAttribute("role1",role1);
        }
        List<Permission> permissions = permissionService.selectAll();
        model.addAttribute("permissions",permissions);
        return "role/input";
    }
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Model  model,Role role,Long[] ids){

            if (role.getId() != null){
                roleService.update(role);
                for (Long pId : ids) {

                    roleService.insertAll(role.getId(),pId);
                }

            }else {
                for (Long pId : ids) {

                    roleService.insert(role);
                    roleService.insertAll(role.getId(), pId);
                }
            }


        return "redirect:/role/list";
    }

    @RequestMapping("/delete")
    public String delete(Long id){
        roleService.delete(id);
        return "redirect:/role/list";
    }


}
